% 名称：Align_Blur_to_Blur.m
% 基于棋盘格，提取配准点，得到模糊图像间的放大率配准参数tformlist3
%
% 作者： 刘昀卓 522543417@qq.com
% 日期： 2022/4/19
%
% 需设置参数：① 棋盘格模糊图片的文件夹路径ImageFolder
%            ② tfrom.mat 文件路径（用于读取BaseWavelength）
%            ③ SaveFolder 结果保存文件夹
%            ④ CalibrateWavelength 矫正波段数组
% 
% 保存：     ① <SaveFolder>/tform3.mat 包含tformlist3与CalibrateWavelength
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clc;clear;close all;
%% 需手动设置的参数
ImageFolder = 'F:\OneDrive\lyz资料\程序资料\实验数据\326数据\202203月底\17_iron\棋盘格'; % 该文件夹需储存固定波段间隔的棋盘格光谱图像与模糊图像
SaveFolder = 'F:\前处理\结果'; % 前处理结果保存文件夹
tformfilename = fullfile('F:\前处理\结果','tform.mat'); % tform文件路径
CalibrateWavelength = [508 700]; % 校正两个额外的输入波长

%% 单帧情况得到一个tform
% 得到基准波长
load(tformfilename,'BaseWavelength');
% 读入基准波长的cb
[imageFileNames,~,WaveLength] =getImageFileNames(fullfile(ImageFolder,'模糊图像'));
cb_base = imread(imageFileNames{WaveLength == BaseWavelength});

for i=1:length(CalibrateWavelength)
    % 读入第i个校准波长的cb
    [imageFileNames,~,WaveLength] =getImageFileNames(fullfile(ImageFolder,'模糊图像'));
    cb_cal = imread(imageFileNames{WaveLength == CalibrateWavelength(i)});
    % 查找bse中的点
    [imagePoints_base,~] = detectCheckerboardPoints(cb_base);
    % 查找cal中的点
    [imagePoints_cal,~] = detectCheckerboardPoints(cb_cal);
    J = insertMarker(cb_cal,imagePoints_cal,'o','Color','red','Size',5);
    J = insertMarker(J,imagePoints_base,'o','Color','yellow','Size',5);
    imshow(J);
    pause(0.5);
    % 得到tform3
    tform3 = fitgeotrans(imagePoints_cal,imagePoints_base,'nonreflectivesimilarity'); 
    tformlist3(i) = tform3; %#ok<SAGROW>
end
% 保存
save(fullfile(SaveFolder,'tform3.mat'),'tformlist3');
save(fullfile(SaveFolder,'tform3.mat'),'CalibrateWavelength','-append');
disp('保存的信息如下:');
whos('-file',fullfile(SaveFolder,'tform3.mat'));

